﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/

using System;
using System.Collections;
using System.Data;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Service.SystemManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.SystemManage
{
    public class UserBusiness
    {
        private readonly UserService _dal = new UserService();

        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool IsExists(UserEntity model)
        {
            return _dal.IsExists(model);
        }

        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Login(UserEntity model)
        {
            var result = _dal.Login(model);
            if (result)
            {
                //根据用户名获取用户信息
                var entity = _dal.GetModel(model.LoginCode);
                //sesstion存储用户信息
                SysContext.SetCurrent(entity.LoginCode, entity.FullName, entity.RoleID.ToString());
            }
            return result;
        }

        /// <summary>
        /// 获取用户分页
        /// </summary>
        /// <param name="qcmodel"></param>
        /// <returns></returns>
        public string GetUserList(UserQcEntity qcmodel)
        {
            var total = _dal.GetUserCount(qcmodel);
            var list = _dal.GetUserList(qcmodel);
            return JsonHelper.Encode(total, list);
        }
        public string GetIsOperatorUserList(UserQcEntity qcmodel)
        {
            var total = _dal.GetIsOperatorUserCount(qcmodel);
            var list = _dal.GetIsOperatorUserList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        /// <summary>
        /// 获取单条用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetUserInfo(string userId)
        {
            return JsonHelper.Encode(_dal.GetUserInfo(userId));
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddUser(string data)
        {
            var model = new UserEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.UserID = Guid.NewGuid();
                model.LoginCode = row["LoginCode"].ToString();
                model.LoginPwd = row["LoginPwd"].ToString();
                model.FullName = row["FullName"].ToString();
                model.MobileNo = row["MobileNo"].ToString();
                model.DptID = new Guid(row["DptID"].ToString());
                model.RoleID = new Guid(row["RoleID"].ToString());
                model.RecStatus = 1;
                model.Desc = row["Desc"].ToString();
            }
            return _dal.AddUser(model);
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="data"></param>
        public bool ModifyUser(string data)
        {
            var model = new UserEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.UserID = new Guid(row["UserID"].ToString());
                model.LoginCode = row["LoginCode"].ToString();
                model.FullName = row["FullName"].ToString();
                model.MobileNo = row["MobileNo"].ToString();
                model.DptID = new Guid(row["DptID"].ToString());
                model.RoleID = new Guid(row["RoleID"].ToString());
                model.Desc = row["Desc"].ToString();
            }
            return _dal.ModifyUser(model);
        }


        public bool LockUser(string userId)
        {
            return _dal.LockUser(userId);
        }

        public bool UnlockUser(string userId)
        {
            return _dal.UnlockUser(userId);
        }

        public bool GetIsNullQCode()
        {
            bool result = false;
            var list = _dal.GetIsNullQCode();
            if (list.Count > 0)
            {
                if (_dal.UpdateQCode(list))
                {
                    result = true;
                }
            }
            return result;
        }

        public int Login(string loginCode, string loginPwd)
        {
            var result = 0;
            var model = new UserEntity { LoginCode = loginCode, LoginPwd = loginPwd };
            if (_dal.CheckedLoginCode(model))
            {
                if (_dal.CheckedRecStatus(model))
                {
                    if (_dal.Login(model))
                    {
                        var entity = _dal.GetModel(model.LoginCode);
                        SysContext.SetCurrent(entity.LoginCode, entity.FullName, entity.RoleID.ToString());
                        result = 3;
                    }
                    else
                    {
                        result = 2;
                    }
                }
                else
                {
                    result = 1;
                }
            }
            else
            {
                result = 0;
            }
            return result;
        }

        public bool ModifyPwd(string data)
        {
            var model = new UserEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.LoginCode = row["LoginCode"].ToString();
                model.LoginPwd = row["NewPwd"].ToString();
            }
            return _dal.ModifyPwd(model);
        }


        public UserEntity UserLogin(UserQcEntity qcmodel)
        {
            return _dal.UserLogin(qcmodel);
        }

        public DataTable ExportUser(UserQcEntity qcmodel)
        {
            var list = _dal.ExportUser(qcmodel);
            return Collection.ListToDataTable(list);
        }
    }
}
